<?php
namespace App\Http\Controllers\Admin;

use App\Models\Admin\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Config;
use App\Models\Admin\Order;

class ProductController extends CommonController
{

//    private $order = new Order();

    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();
    }

    /**
     * 商品回收积分详情
     */
    public function product_recovery(Request $request){
        $keyword = $request->input('keyword' , '');
        $productModel = new Product();

        $where = [];
        if($keyword){
            $where[] = ['product_name' , 'like' , '%'.$keyword.'%'];
        }

        $product = $productModel->get_all_product($where);
        foreach($product as &$v){
            $v->recovery = $productModel->recovery($v->product_id);
        }
        return view('admin.product.product_recovery',['product'=>$product , 'keyword'=>$keyword]);
    }

    /**
     * 商品购买订单列表
     * @param Request $request
     */
    public function order_list(Request $request){
        $product_id = $request->input('product_id');
        $order = DB::table('tbuy_order_details t')
                    ->where('t.product_id' , $product_id)
                    ->where('t.status' , 1)
                    ->leftJoin('tbuy_order tor','t.order_id' , '=' , 'tor.order_id')
                    ->select('tor.order_id','tor.order_no','tor.pay_type_group','tor.amount','tor.create_time','t.product_name','tor.pay_time')
                    ->paginate(Config::get('app.pagesize'));
        $orderModel = new Order();
        foreach($order as &$v){
            $v->pay_type = $orderModel->parsePayType($v->pay_type_group);

            $pay = '';
            foreach($v->pay_type as $k=>$p){
                $pay .= $p['name'] . ':' . $p['amount'] . ' ';
            }
            $v->pay_type = $pay;
        }
        return view('admin.product.order_list' , ['order'=>$order]);
    }
}
